package com.unsiv.torch;

import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
/**
 * <p>edit pannel</p>
 * @author fanzhen
 * @time   4:44:21 PM Sep 9, 2011
 * @email  fanzhen.meng@gmail.com
 */
public class EditPannel extends Activity {
	private String current_model = "brightness";
	private int bright_model_value=5;
	private int strobe_model_value=0;
	private ImageView brightness;
	private ImageView strobe;
	private ImageView customize;
	private LinearLayout brightness_l;
	private LinearLayout strobe_l;
	private RelativeLayout customize_l;
	private ImageView done;
	private ImageView cancel;
	private ImageView tap;
	private ImageView ibutton_information;
	private ImageView ibutton_help;
	private SeekBar sb_brightness;
	private SeekBar sb_strobe;
	private TextView brightness_progress;
	private TextView strobe_progress;
	private SharedPreferences setting;
	private SharedPreferences mode;
	private Editor settingEditor;
	private Editor modelEditor;
	private String type;
	private AdView adView;
//	private boolean tapstatus = false;
	private long lastTapTime;
//	private long tapperiod;
	private long countStart;
	private long countEnd;
	private String tapValue = "customize";
	private String current_value = "";
	private String mode_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.edit_pannel);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
				WindowManager.LayoutParams.FLAG_FULLSCREEN);  
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
		ImageView window_title = (ImageView)findViewById(R.id.window_title_name);
		window_title.setBackgroundResource(R.drawable.title_programming);
		// // Create the adView
		adView = (AdView) findViewById(R.id.adView);
		// // Initiate a generic request to load it with an ad
		adView.loadAd(new AdRequest());
		brightness_l = (LinearLayout)findViewById(R.id.screen_line_brightness);
		strobe_l = (LinearLayout)findViewById(R.id.screen_line_strobe);
		customize_l = (RelativeLayout)findViewById(R.id.screen_line_customize);
		brightness = (ImageView)findViewById(R.id.screen_edit_brightness);
		strobe = (ImageView)findViewById(R.id.screen_edit_strobe);
		customize = (ImageView)findViewById(R.id.screen_edit_customize);
		sb_brightness = (SeekBar)findViewById(R.id.sb_brightness);
		sb_strobe = (SeekBar)findViewById(R.id.sb_strobe);
		done = (ImageView)findViewById(R.id.done);
		cancel = (ImageView)findViewById(R.id.cancel);
		tap = (ImageView)findViewById(R.id.tap);
		ibutton_information = (ImageView) findViewById(R.id.program_information);
		ibutton_help = (ImageView) findViewById(R.id.program_help);
		brightness_progress = (TextView)findViewById(R.id.brightness_progress);
		strobe_progress = (TextView)findViewById(R.id.strobe_progress);
		sb_strobe.setEnabled(false);
		setting = getSharedPreferences("setting", 0);
		settingEditor = setting.edit();
		init();
		modelEditor = mode.edit();
		brightness_l.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				current_model = "brightness";
				brightness_l.setBackgroundResource(R.drawable.edit_bg);
				strobe_l.setBackgroundResource(R.drawable.edit_bg_no);
				customize_l.setBackgroundResource(R.drawable.edit_bg_no);
				brightness.setImageResource(R.drawable.brightness_on);
				strobe.setImageResource(R.drawable.strobe_off);
				customize.setImageResource(R.drawable.customize_off);
				tap.setVisibility(View.INVISIBLE);
				sb_brightness.setEnabled(true);
				sb_strobe.setEnabled(false);
			}
		});
		strobe_l.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				current_model = "strobe";
				brightness_l.setBackgroundResource(R.drawable.edit_bg_no);
				customize_l.setBackgroundResource(R.drawable.edit_bg_no);
				strobe_l.setBackgroundResource(R.drawable.edit_bg);
				brightness.setImageResource(R.drawable.brightness_off);
				strobe.setImageResource(R.drawable.strobe_on);
				customize.setImageResource(R.drawable.customize_off);
				tap.setVisibility(View.INVISIBLE);
				sb_strobe.setEnabled(true);
				sb_brightness.setEnabled(false);
			}
		});
		customize_l.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				current_model = "customize";
				customize_l.setBackgroundResource(R.drawable.edit_bg);
				brightness_l.setBackgroundResource(R.drawable.edit_bg_no);
				strobe_l.setBackgroundResource(R.drawable.edit_bg_no);
				brightness.setImageResource(R.drawable.brightness_off);
				strobe.setImageResource(R.drawable.strobe_off);
				customize.setImageResource(R.drawable.customize_on);
				tap.setVisibility(View.VISIBLE);
			}
		});
		sb_brightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar sb) {
				brightness_progress.setText(String.valueOf(sb.getProgress()+5));
				bright_model_value = sb.getProgress()+5;
			}
			@Override
			public void onStartTrackingTouch(SeekBar sb) {
			}
			@Override
			public void onProgressChanged(SeekBar sb, int arg1, boolean arg2) {
			
			}
		});
		sb_strobe.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar sb) {
				strobe_progress.setText(String.valueOf((float)(sb.getProgress()+5)/10));
				strobe_model_value = sb.getProgress();
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar sb) {
			}
			
			@Override
			public void onProgressChanged(SeekBar sb, int arg1, boolean arg2) {
				
			}
		});
		tap.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View view, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					countStart =  System.currentTimeMillis();
					if("led".equalsIgnoreCase(type)){
						MyCamaraHelper.turnOn();
						tap.setBackgroundResource(R.drawable.tap_light);
					}else{
						tap.setBackgroundResource(R.drawable.tap_light);
					}
					break;
				case MotionEvent.ACTION_UP:
					if("led".equalsIgnoreCase(type)){
						MyCamaraHelper.turnOff();
						tap.setBackgroundResource(R.drawable.tap);
					}else{
						tap.setBackgroundResource(R.drawable.tap);
					}
					countEnd =  System.currentTimeMillis();
					if(countStart>0){
						long light_tapperiod = countEnd - countStart;
						tapValue = tapValue +"," + light_tapperiod;
					}
					if(lastTapTime>0){
						long black_tapperiod = countStart - lastTapTime;
						tapValue = tapValue +"," + black_tapperiod;
					}
					lastTapTime = countEnd;
				default:
					break;
				}
				return true;
			}
		});
//		tap.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View arg0) {
//				long current = System.currentTimeMillis();
//				tapperiod = current - lastTapTime;
//				if(lastTapTime>0){
//					tapValue = tapValue +"," + tapperiod;
//				}
//				lastTapTime = current;
//				if("led".equalsIgnoreCase(type)){
//					if(tapstatus){
//						MyCamaraHelper.turnOff();
//						tap.setBackgroundResource(R.drawable.tap);
//						tapstatus = false;
//					}
//					else{
//						MyCamaraHelper.turnOn();
//						tap.setBackgroundResource(R.drawable.tap_light);
//						tapstatus = true;
//					}
//				}else{
//					if(tapstatus){
//						tap.setBackgroundResource(R.drawable.tap);
//						tapstatus = false;
//					}else{
//						tap.setBackgroundResource(R.drawable.tap_light);
//						tapstatus = true;
//					}
//				}
//			}
//		});
		done.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				boolean newCustom = false;
				if("brightness".equalsIgnoreCase(current_model)){
					current_value = "brightness"+","+String.valueOf(bright_model_value);
				}
				if("strobe".equalsIgnoreCase(current_model)){
					current_value = "strobe"+","+String.valueOf(strobe_model_value);
				}
				if("customize".equalsIgnoreCase(current_model)){
					current_value = tapValue;
					newCustom = true;
				}
				if(!"".equalsIgnoreCase(current_value)){
					Map<String,?> models = mode.getAll();
					int nextId = models.size()+1;
					if(mode_id!=null){
						nextId = Integer.parseInt(mode_id);
					}
					modelEditor.putString(String.valueOf(nextId), current_value);
					modelEditor.commit();
					if("led".equalsIgnoreCase(type)){
						settingEditor.putInt("current_led_mode",nextId);
					}else{
						settingEditor.putInt("current_screen_mode",nextId);
					}
					settingEditor.commit();
				}
				Intent intent = new Intent(getApplication(), Program.class);
				intent.putExtra("type", type);
				intent.putExtra("newcustom", newCustom);
				startActivity(intent);
			}
		});
		cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(getApplication(), Program.class);
				intent.putExtra("type", type);
				startActivity(intent);
			}
		});
		ibutton_information.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplication(), Information.class);
				startActivity(intent);
			}
		});
		ibutton_help.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(getApplication(), Help.class);
				startActivity(intent);
			}
		});
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	private void init(){
		Intent intent = getIntent();
		type = intent.getStringExtra("type");
		mode_id = intent.getStringExtra("mode_id");
		if("led".equalsIgnoreCase(type)){
			mode = getSharedPreferences("led_mode", 0);
		}else{
			mode = getSharedPreferences("screen_mode", 0);
		}
		if(mode_id!=null){
			String value = mode.getString(mode_id, "");
			current_value = value;
			String values[] = value.split(",");
			if("customize".equals(values[0])){
				current_model = "customize";
				customize_l.setBackgroundResource(R.drawable.edit_bg);
				brightness_l.setBackgroundResource(R.drawable.edit_bg_no);
				strobe_l.setBackgroundResource(R.drawable.edit_bg_no);
				brightness.setImageResource(R.drawable.brightness_off);
				strobe.setImageResource(R.drawable.strobe_off);
				customize.setImageResource(R.drawable.customize_on);
				tap.setVisibility(View.VISIBLE);
			}else if("strobe".equals(values[0])){
				current_model = "strobe";
				brightness_l.setBackgroundResource(R.drawable.edit_bg_no);
				customize_l.setBackgroundResource(R.drawable.edit_bg_no);
				strobe_l.setBackgroundResource(R.drawable.edit_bg);
				brightness.setImageResource(R.drawable.brightness_off);
				strobe.setImageResource(R.drawable.strobe_on);
				customize.setImageResource(R.drawable.customize_off);
				tap.setVisibility(View.INVISIBLE);
				sb_strobe.setEnabled(true);
				sb_brightness.setEnabled(false);
				strobe_model_value = Integer.parseInt(values[1]);
				strobe_progress.setText(String.valueOf((float)(Float.parseFloat(values[1])+5)/10));
				sb_strobe.setProgress(Integer.parseInt(values[1]));
			}else{
				current_model = "brightness";
				brightness_l.setBackgroundResource(R.drawable.edit_bg);
				strobe_l.setBackgroundResource(R.drawable.edit_bg_no);
				customize_l.setBackgroundResource(R.drawable.edit_bg_no);
				brightness.setImageResource(R.drawable.brightness_on);
				strobe.setImageResource(R.drawable.strobe_off);
				customize.setImageResource(R.drawable.customize_off);
				tap.setVisibility(View.INVISIBLE);
				sb_brightness.setEnabled(true);
				sb_strobe.setEnabled(false);
				bright_model_value = Integer.parseInt(values[1]);
				brightness_progress.setText(values[1]);
				sb_brightness.setProgress(Integer.parseInt(values[1])-5);
			}
		}
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, "Exit");
		return true;
	}
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			MyCamaraHelper.turnOff();
			new ActivityUtil(this).exit();
			return true;
		}
		return false;
	}
}